#!/usr/bin/env python

import max.file
import max.stat
import max.web

def info(msg):
	print "[i] " + msg

def update(r):
	if r == "feedings":
		data_feedings.append(max.file.update_feedings())
	elif r == "wet":
		data_wet.append(max.file.update_wet())
	elif r == "poopy":
		data_poopy.append(max.file.update_poopy())
	elif r == "unknown":
		data_unknown.append(max.file.update_unknown())
	stats = max.stat.build(data_feedings,data_wet,data_poopy,data_unknown)
	return stats

# main routine
info("starting")

# global variables
data_feedings = []
data_wet = []
data_poopy = []
data_unknown = []

# build the data structures from the files
info("data:")
data_feedings, data_wet, data_poopy, data_unknown = max.file.read_data()
info("\t" + str(len(data_feedings)) + " feeding records")
info("\t" + str(len(data_wet)) + " wet records")
info("\t" + str(len(data_poopy)) + " poopy records")
info("\t" + str(len(data_unknown)) + " unknown records")

# build statistics
info("statistics:")
stats = max.stat.build(data_feedings,data_wet,data_poopy,data_unknown)

# start the webserver, which calls handlers and shouldn't return
info("webserver:")
max.web.run(stats,update) #doesn't return

